var starttime = "";
var adultnum = 0;
var kidsnum = 0;
var linetime = "";
var adultprice = 0;
var kidsprice = 0;
var totalprice = 0;
var lineid = "";
var carid = "";
var time = "";
var firstkey = 0;
$(document).ready(function(){
	pageHelpjs();
	OnLoadP();
	WantLineinfo();
	ChooseNumPrice();
	WantLineA();
	WantLineAr();
	WantLineR();
	ChooseMoreD();
	GotoO();
	WantOneYearMonth();
});
//得到项目目录
function pageHelpjs(){
	var strFullPath=window.document.location.href;  
    var strPath=window.document.location.pathname;  
    var pos=strFullPath.indexOf(strPath);  
    var prePath=strFullPath.substring(0,pos);  
    var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);  
    var basePath = prePath;  
    basePath = prePath + postPath;//获取项目根目录
    imgb=basePath+"/static/pcsofangche/line/lineinfo/img/";
}
//跳转线路填写订单页
function GotoO(){
	$("input.lineinfo_info_right_ordernow_b").click(function(){
		if(totalprice > 0){			
			window.location.href = (basePath+"/lineorder?lineadultnum="+adultnum+"&linekidsnum="+kidsnum+"&linetime="+linetime+"&linecarid="+carid);
		}else{
			alert("请选择具体日期及人数");
		}
	});
}
//初始化参数
function OnLoadP(){
	lineid = $("font.hidden>font.hidden:eq(0)").text();
}
//点击显示或隐藏行程介绍，行程安排，预定须知
function ChooseMoreD(){
	//锚点跳转
	$("div.lineinfo_detail_choose_item:eq(0)").click(function(){
//		$("div.lineinfo_detail_introall").fadeToggle();
		window.location.hash = "#introall";
	});
	$("div.lineinfo_detail_choose_item:eq(1)").click(function(){
//		$("div.lineinfo_detail_arangeall").fadeToggle();
		window.location.hash = "#arangeall";
	});
	$("div.lineinfo_detail_choose_item:eq(2)").click(function(){
//		$("div.lineinfo_detail_ruleall").fadeToggle();
		window.location.hash = "#ruleall";
	});
	
}
//随着滚轮滑动形成介绍，行程安排，预定须知
function RollAction(){
	var win = $(window);
	var sc = $(document);
	var onet = $("div.lineinfo_detail_introall").outerHeight();
	var twot = $("div.lineinfo_detail_arangeall").outerHeight();
	console.log("onet="+onet);
	console.log("twot="+twot);
	var height = sc.scrollTop();
	win.scroll(function(){
		var height = sc.scrollTop();
		if(height<1100){
			console.log("length="+sc.scrollTop());
			$("div.lineinfo_detail_choose").removeClass("lineinfo_detail_choosefix");

			$("div.lineinfo_detail_choose_item:eq(0)").addClass("lineinfo_detail_choose_itemchoose");
			$("div.lineinfo_detail_choose_item:eq(1)").removeClass("lineinfo_detail_choose_itemchoose");
			$("div.lineinfo_detail_choose_item:eq(2)").removeClass("lineinfo_detail_choose_itemchoose");
		}
		if(height>=1100 && height<(1100+onet)){
			console.log("length="+sc.scrollTop());
			$("div.lineinfo_detail_choose").addClass("lineinfo_detail_choosefix");
			
		}
		if(height>=(1100+onet) && height<(1100+onet+twot)){
			console.log("length="+sc.scrollTop());
			$("div.lineinfo_detail_choose_item:eq(0)").removeClass("lineinfo_detail_choose_itemchoose");
			$("div.lineinfo_detail_choose_item:eq(1)").addClass("lineinfo_detail_choose_itemchoose");
			$("div.lineinfo_detail_choose_item:eq(2)").removeClass("lineinfo_detail_choose_itemchoose");
			
		}
		if(height>=(1100+onet+twot)){
			console.log("length="+sc.scrollTop());
			$("div.lineinfo_detail_choose_item:eq(0)").removeClass("lineinfo_detail_choose_itemchoose");
			$("div.lineinfo_detail_choose_item:eq(1)").removeClass("lineinfo_detail_choose_itemchoose");
			$("div.lineinfo_detail_choose_item:eq(2)").addClass("lineinfo_detail_choose_itemchoose");
		}
		
	});
}
//点击车型请求日历 
function ChooseCal(){
	
	var l = $("div.lineinfo_info_right_choose_right_item").length;
	for(var i = 0;i < l;i++){
		$("div.lineinfo_info_right_choose_right_item:eq("+i+")").click(function(i){
			return function(){
				carid = $("div.lineinfo_info_right_choose_right>font:eq("+i+")").text();
				WantCal(time,carid);
				$(this).addClass("lineinfo_info_right_choose_right_itemchoose");
				$("div.lineinfo_info_right_choose_right>div").not(this).removeClass("lineinfo_info_right_choose_right_itemchoose");
			};
		}(i))
	}
}
//点击日期选择成人儿童价格 
function ChoosePrice(){
	var l = $("div.lineinfo_info_left_cal_dayprice_item").length;
	for(var i = 0;i<l;i++){
		$("div.lineinfo_info_left_cal_dayprice_item:eq("+i+")").click(function(i){
			return function(){
				adultprice = $("div.lineinfo_info_left_cal_dayprice_item:eq("+i+") div.lineinfo_info_left_cal_dayprice_item_price_two:eq(0)").text();
				kidsprice = $("div.lineinfo_info_left_cal_dayprice_item:eq("+i+") div.lineinfo_info_left_cal_dayprice_item_price_two:eq(1)").text();
				linetime = $("div.lineinfo_info_left_cal_dayprice_item:eq("+i+") div.lineinfo_info_left_cal_dayprice_item_date_oneh:eq(0)").text();
				var now = new Date();
				var time = linetime + " 00:00:00";
				var choosetime  = new Date(time);
				if(choosetime > now){
					totalprice = adultnum * adultprice + kidsnum * kidsprice;
					$("div.lineinfo_info_right_price_price_three").text(totalprice);
					$("div.lineinfo_info_right_gotime_right").text(linetime);
					$(this).addClass("lineinfo_info_left_cal_dayprice_itemchoose");
					$("div.lineinfo_info_left_cal_dayprice_item").not(this).removeClass("lineinfo_info_left_cal_dayprice_itemchoose");
					console.log("adultprice="+adultprice);
					console.log("kidsprice="+kidsprice);
					console.log("linetime="+linetime);
				}else{
					alert("请选择正确的时间");
				}
			}
		}(i));
	}
}
//选择成人儿童的个数
function ChooseNumPrice(){
	$("div.lineinfo_info_right_gonum_right_item_two:eq(0)").click(function(){
		adultnum --;
		if(adultnum <= 0){
			adultnum = 0;
		};
		$("div.lineinfo_info_right_gonum_right_item_three:eq(0)").text(adultnum);
		totalprice = adultnum * adultprice + kidsnum * kidsprice;
		$("div.lineinfo_info_right_price_price_three").text(totalprice);
	});
	$("div.lineinfo_info_right_gonum_right_item_two:eq(1)").click(function(){
		adultnum ++;
		if(adultnum <= 0){
			adultnum = 0;
		};
		$("div.lineinfo_info_right_gonum_right_item_three:eq(0)").text(adultnum);
		totalprice = adultnum * adultprice + kidsnum * kidsprice;
		$("div.lineinfo_info_right_price_price_three").text(totalprice);
	});
	$("div.lineinfo_info_right_gonum_right_item_two:eq(2)").click(function(){
		kidsnum --;
		if(kidsnum <= 0){
			kidsnum = 0;
		};
		$("div.lineinfo_info_right_gonum_right_item_three:eq(1)").text(kidsnum);
		totalprice = adultnum * adultprice + kidsnum * kidsprice;
		$("div.lineinfo_info_right_price_price_three").text(totalprice);
	});
	$("div.lineinfo_info_right_gonum_right_item_two:eq(3)").click(function(){
		kidsnum ++;
		if(kidsnum <= 0){
			kidsnum = 0;
		};
		$("div.lineinfo_info_right_gonum_right_item_three:eq(1)").text(kidsnum);
		totalprice = adultnum * adultprice + kidsnum * kidsprice;
		$("div.lineinfo_info_right_price_price_three").text(totalprice);
	});
}
//请求线路详情
function WantLineinfo(){
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/line/lineinfobaseinfo",
		data : {
			lineid:lineid
		},
		contentType : "application/x-www-form-urlencoded;charset=utf-8",

		success : function(data) {
			var pictureone = data.data.pic[0].name;
			console.log("pictureone="+pictureone);
			$("img#bannerimg1").attr("src",pictureone);
			for(var i =1;i<data.data.pic.length;i++){
//				var a="<li data-target=\"#banner\" data-slide-to=\""+i+"\"></li>"
//				$("ol.carousel-indicators>li.active").after(a);
				var b="<div class=\"item\">\n" +
				"<img src=\""+data.data.pic[i].name+"\" alt=\"First slide\" class=\"img-responsive\" />\n" +
				"<div class=\"carousel-caption\" ></div>\n" +
				"</div>"
				$("div.carousel-inner>div.active").after(b);
				console.log("picture="+data.data.pic[i].name);
			}
			var p ="";
			for(var i=0;i<data.data.path.length-1;i++){
				p = p + "<div class=\"lineinfo_info_right_path_right_item\">"+data.data.path[i].name+"</div>"+
							"<div class=\"lineinfo_info_right_path_right_itemna\">></div>"
			}
			p = p + "<div class=\"lineinfo_info_right_path_right_item\">"+data.data.path[data.data.path.length-1].name+"</div>";
			$("div.lineinfo_info_right_path_right").html(p);
			var b = "";
			for(var i=0;i<data.data.bran.length;i++){
				b = b + "<div class=\"lineinfo_info_right_choose_right_item shand\">"+data.data.bran[i].car+"</div>"+
					"<font class=\"hidden\">"+data.data.bran[i].carid+"</font>"
			}
			$("div.lineinfo_info_right_choose_right").html(b);
			var ch = "";
			for(var i=0;i<data.data.cha.length;i++){
				ch = ch + "<div class=\"lineinfo_info_right_idea_right_item\">"+data.data.cha[i].name+"</div>"
			}
			$("div.lineinfo_info_right_idea_right").html(ch);
			var title = data.data.title;
			$("div.lineinfo_info_right_title").text(title);
			var city = data.data.city;
			$("div.lineinfo_info_right_city_right").text(city);
			var days = data.data.days;
			$("div.lineinfo_info_right_days_right").text(days);
			var codenum = data.data.codenum;
			$("div.lineinfo_info_right_price_num_right").text(codenum);
			
			ChooseCal();
			//第一次请求时顺带请求价格日历
			if(firstkey == 0){
				carid = data.data.bran[0].carid;//carid默认第一个
				WantTMCal();
				firstkey = 1;
			}
			//设置title
			$(document).attr("title",title);//修改title值  
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}
//默认情况下请求当月日历
function WantTMCal(){
	var mydate = new Date();
	var year = mydate.getFullYear();
	var month = mydate.getMonth() + 1;
	if(month<10){
		month = "0" + month;
	}
	time = year + "-" + month;
	console.log("time="+time);
	console.log("carid="+carid);	
	WantCal(time,carid);
	
}
//请求价格日历
function WantCal(time,carid){
	LoadingIcon = layer.load();
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/line/lineinfodateprice",
		data : {
			time:time,
			carid:carid
		},
		contentType : "application/x-www-form-urlencoded;charset=utf-8",

		success : function(data) {
			var c = "";
			for(var i=0;i<data.data.length;i++){
				if(data.data[i].adult != "" && data.data[i].kid != ""){
					c = c +  "<div class=\"lineinfo_info_left_cal_dayprice_item shand\">\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_date clearfix\">\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_date_oneh\">"+data.data[i].date+"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_date_one\">"+data.data[i].sybdate+"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_date_oneh\">号</div>\n" +
					"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price clearfix\">\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_oneup\">起</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_two\">"+data.data[i].adult+"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_one\">成￥</div>\n" +
					"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price clearfix\">\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_oneup\">起</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_two\">"+data.data[i].kid+"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_one\">儿￥</div>\n" +
					"</div>\n" +
					"</div>"
				}else{
					c = c +  "<div class=\"lineinfo_info_left_cal_dayprice_nochoose\">\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_date clearfix\">\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_date_oneh\">"+data.data[i].date+"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_date_one\">"+data.data[i].sybdate+"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_date_one\"></div>\n" +
					"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price clearfix\">\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_one\"></div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_one\">"+data.data[i].adult+"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_one\"></div>\n" +
					"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price clearfix\">\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_one\"></div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_one\">"+data.data[i].kid+"</div>\n" +
					"<div class=\"lineinfo_info_left_cal_dayprice_item_price_one\"></div>\n" +
					"</div>\n" +
					"</div>"
				}
				
			};
			$("div.lineinfo_info_left_cal_dayprice").html(c);
			ChoosePrice();
			//因为每次请求价格日历介绍用时最长，所以将滚轮滑动动作触发器放与此
			RollAction();
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
			layer.close(LoadingIcon);
		},
	});
}
//请求线路行程介绍
function WantLineA(){
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/line/lineinfointro",
		data : {
			lineid:lineid
		},
		contentType : "application/x-www-form-urlencoded;charset=utf-8",

		success : function(data) {
			if(data.data[0].sign === "1"){
				var a = "";
				for(var i = 0;i<data.data.length;i++){
					if(data.data[i].type == "标题"){
						a = a + "<div class=\"lineinfo_detail_intro_title\">"+data.data[i].detail+"</div>";
					}else if(data.data[i].type == "内容"){
						a = a + "<div class=\"lineinfo_detail_intro_p\">"+data.data[i].detail+"</div>";
					}else{
						a = a + "<div class=\"lineinfo_detail_intro_img\">"+
							"<img src=\""+data.data[i].detail+"\" class=\"lineinfo_detail_intro_img_posi\">"+
							"</div>"
					};
					console.log("a1="+a);
					$("div.lineinfo_detail_intro").html(a);
				}
			}else{
				var a = "";
				for(var i = 0;i<data.data.length;i++){
					a = a + data.data[i].infodetail;
				};
				console.log("a2="+a);
				$("div.lineinfo_detail_intro").html(a);
			};
			
			
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}
//请求线路行程安排
function WantLineAr(){
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/line/lineinfodayarange",
		data : {
			lineid:lineid
		},
		contentType : "application/x-www-form-urlencoded;charset=utf-8",

		success : function(data) {
			var a = "";
			var b = "";
			for(var i =0;i<data.data.length;i++){
			
				for(var j = 0;j<data.data[i].detail.length;j++){
					if(data.data[i].detail[j].type == "吃"){
						b = b +"<div class=\"lineinfo_detail_arange_item_detail_item clearfix\">"+
								"<div class=\"lineinfo_detail_arange_item_detail_item_one\">"+data.data[i].detail[j].time+"</div>"+
								"<div class=\"lineinfo_detail_arange_item_detail_item_two\">"+
									"<img src=\""+imgb+"chi.png\" class=\"lineinfo_detail_arange_item_detail_item_two_posi\">"+
								"</div>"+
								"<div class=\"lineinfo_detail_arange_item_detail_item_three\">"+data.data[i].detail[j].detail+"</div>"+
								"</div>";
					}else if(data.data[i].detail[j].type == "行"){
						b = b +"<div class=\"lineinfo_detail_arange_item_detail_item clearfix\">"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_one\">"+data.data[i].detail[j].time+"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_two\">"+
							"<img src=\""+imgb+"xing.png\" class=\"lineinfo_detail_arange_item_detail_item_two_posi\">"+
						"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_three\">"+data.data[i].detail[j].detail+"</div>"+
						"</div>";
					}else if(data.data[i].detail[j].type == "游"){
						b = b +"<div class=\"lineinfo_detail_arange_item_detail_item clearfix\">"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_one\">"+data.data[i].detail[j].time+"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_two\">"+
							"<img src=\""+imgb+"you.png\" class=\"lineinfo_detail_arange_item_detail_item_two_posi\">"+
						"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_three\">"+data.data[i].detail[j].detail+"</div>"+
						"</div>";
					}else if(data.data[i].detail[j].type == "娱"){
						b = b +"<div class=\"lineinfo_detail_arange_item_detail_item clearfix\">"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_one\">"+data.data[i].detail[j].time+"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_two\">"+
							"<img src=\""+imgb+"yu.png\" class=\"lineinfo_detail_arange_item_detail_item_two_posi\">"+
						"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_three\">"+data.data[i].detail[j].detail+"</div>"+
						"</div>";
					}else if(data.data[i].detail[j].type == "住"){
						b = b +"<div class=\"lineinfo_detail_arange_item_detail_item clearfix\">"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_one\">"+data.data[i].detail[j].time+"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_two\">"+
							"<img src=\""+imgb+"zhu.png\" class=\"lineinfo_detail_arange_item_detail_item_two_posi\">"+
						"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_three\">"+data.data[i].detail[j].detail+"</div>"+
						"</div>";
					}else{
						b = b +"<div class=\"lineinfo_detail_arange_item_detail_item clearfix\">"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_one\">"+data.data[i].detail[j].time+"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_two\">"+
							"<img src=\""+imgb+"gou .png\" class=\"lineinfo_detail_arange_item_detail_item_two_posi\">"+
						"</div>"+
						"<div class=\"lineinfo_detail_arange_item_detail_item_three\">"+data.data[i].detail[j].detail+"</div>"+
						"</div>";
					}
				}
				a = a +"<div class=\"lineinfo_detail_arange_item\">"+
				"<div class=\"lineinfo_detail_arange_item_allday clearfix\">"+
					"<div class=\"lineinfo_detail_arange_item_allday_left\">"+data.data[i].day+"</div>"+
					"<div class=\"lineinfo_detail_arange_item_allday_right\">"+data.data[i].dayjourney+"</div>"+
				"</div>"+
				"<div class=\"lineinfo_detail_arange_item_detail\">"+
					b+
				"</div>"+
				"</div>";
			};
			$("div.lineinfo_detail_arange").html(a);
			
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}
//请求线路预定须知
function WantLineR(){
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/line/lineinforule",
		data : {
			lineid:lineid
		},
		contentType : "application/x-www-form-urlencoded;charset=utf-8",

		success : function(data) {
			var r = "";
			for(var i =0;i<data.data.length;i++){
				if(data.data[i].type == "标题"){
					r = r + "<div class=\"lineinfo_detail_rule_item\">"+
						"<font class=\"hidden\">"+data.data[i].detail+"</font>"+
						"</div>"
				}else{
					r = r + "<div class=\"lineinfo_detail_rule_item\">"+
					"<font class=\"hidden\">"+data.data[i].detail+"</font>"+
					"</div>"
				};
			};
			$("div.lineinfo_detail_rule").html(r);
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}
//请求当前尽1年的月份
function WantOneYearMonth(){
	$.ajax({
		dataType : "json",
		type : "post",
		url : basePath+"/car/rentinfogivemonth",
		data : {
			
		},
		contentType : "application/x-www-form-urlencoded;charset=utf-8",

		success : function(data) {
			var r = "";
			for(var i =0;i<data.data.length;i++){
				r = r + "<div class=\"lineinfo_info_left_cal_choose_item\" data-month=\""+data.data[i].month+"\">"+data.data[i].month+"</div>"
			};
			$("div.lineinfo_info_left_cal_choose").html(r);
			//初始载入时候默认选择当月日历
			$("div.lineinfo_info_left_cal_choose_item:eq(0)").addClass("lineinfo_info_left_cal_choose_itemchoose");
			//点击月份时选择日历
			$("div.lineinfo_info_left_cal_choose_item").click(function(){
				$(this).addClass("lineinfo_info_left_cal_choose_itemchoose");
				$("div.lineinfo_info_left_cal_choose>div").not(this).removeClass("lineinfo_info_left_cal_choose_itemchoose");
				time = $(this).data("month");
				WantCal(time,carid);
			});
		},
		error : function() {
			console.log("访问失败");
		},
		complete : function() {
			console.log("请求完成");
		},
	});
}